package com.grapecity.circlefriend.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.grapecity.circlefriend.R;
import com.grapecity.control.gridview.ImageAdapter;
import com.grapecity.control.listview.CircleListView;
import com.grapecity.control.listview.item.CircleComment;
import com.grapecity.control.listview.item.CircleItem;
import com.grapecity.control.listview.item.CircleItemView;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	// view
	private CircleListView listView;
	private View commentView;

	// adaper
	private CircleAdapter adapter;

	// msg
	private ArrayList<CircleItem> msgList = new ArrayList<CircleItem>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		commentView = findViewById(R.id.commentView);

		init();
		adapter = new CircleAdapter(MainActivity.this, msgList);
		listView = (CircleListView) findViewById(R.id.circleListView);
		listView.setAdapter(adapter);
	}

	private void init() {
		String imgHeader = "http://img1.imgtn.bdimg.com/it/u=2527447014,327543745&fm=21&gp=0.jpg";
		String[] imgs = new String[1];
		imgs[0] = "http://hi.csdn.net/attachment/201110/30/0_1319976939pkgr.gif";
		CircleItem item1 = new CircleItem(imgHeader, "发的发生地方萨芬撒打算放大", "今天天气真好!范德萨发撒范德萨发生法撒旦飞洒发生的发生发生的发生分散", imgs, "",
				"1小时前");
		for (int i = 0; i < 1; i++) {
			msgList.add(item1);
		}
		CircleComment comment = new CircleComment("小龙女", "是啊，天气真好！");
		item1.getComments().add(comment);
		item1.getComments().add(comment);
		item1.getComments().add(comment);
		item1.getComments().add(comment);

		String[] imgs2 = new String[7];
		imgs2[0] = "http://img1.imgtn.bdimg.com/it/u=2527447014,327543745&fm=21&gp=0.jpg";
		imgs2[1] = "https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=71cd4229be014a909e3e41bd99763971/472309f7905298221dd4c458d0ca7bcb0b46d442.jpg";
		imgs2[2] = "http://img0.imgtn.bdimg.com/it/u=1185995358,925786236&fm=21&gp=0.jpg";
		imgs2[3] = "http://img4.imgtn.bdimg.com/it/u=2718132976,3419769724&fm=21&gp=0.jpg";
		imgs2[4] = "http://img4.imgtn.bdimg.com/it/u=1084883328,2041867738&fm=21&gp=0.jpg";
		imgs2[5] = "http://img1.imgtn.bdimg.com/it/u=3720795902,3920304027&fm=21&gp=0.jpg";
		imgs2[6] = "http://img2.imgtn.bdimg.com/it/u=2420500865,658636329&fm=21&gp=0.jpg";

		CircleItem item2 = new CircleItem("http://img2.imgtn.bdimg.com/it/u=1245978318,2845210478&fm=21&gp=0.jpg",
				"方法反反复复反复", "今天天气真好!", imgs2, "", "2小时前");
		for (int i = 0; i < 1; i++) {
			msgList.add(item2);
		}

		String[] imgs3 = new String[2];
		imgs3[0] = "http://img4.imgtn.bdimg.com/it/u=3814971924,4161415886&fm=11&gp=0.jpg";
		imgs3[1] = "http://img3.imgtn.bdimg.com/it/u=1681882397,3535453166&fm=11&gp=0.jpg";

		CircleItem item3 = new CircleItem("http://img2.imgtn.bdimg.com/it/u=285181029,2204041205&fm=21&gp=0.jpg",
				"linux", "hello world!\nhello world!\nhello world!\nhello world!\nhello world!\nhello world!", imgs3,
				"中共中央总书记、国家主席、中央军委主席习近平在六安市金寨县花石乡大湾村村民陈泽申家同村民们亲切交流。", "5小时前");
		for (int i = 0; i < 1; i++) {
			msgList.add(item3);
		}

		String[] imgs4 = new String[4];
		imgs4[0] = "http://img4.imgtn.bdimg.com/it/u=2718132976,3419769724&fm=21&gp=0.jpg";
		imgs4[1] = "http://img4.imgtn.bdimg.com/it/u=1084883328,2041867738&fm=21&gp=0.jpg";
		imgs4[2] = "http://img1.imgtn.bdimg.com/it/u=3720795902,3920304027&fm=21&gp=0.jpg";
		imgs4[3] = "http://img2.imgtn.bdimg.com/it/u=2420500865,658636329&fm=21&gp=0.jpg";
		CircleItem item4 = new CircleItem("http://img4.imgtn.bdimg.com/it/u=2718132976,3419769724&fm=21&gp=0.jpg", "小号",
				"参考相关研究， 对不同岗位， 有不同的能力模型。 大家可以针对自己岗位的能力模型， 利用附件的面试指南， 针对自己岗位要求的能力， 做一下自我面试。按照人才发展理论， 先了解自我， 然后接受自我并思考， 进而就采取行动并不断发展。 不错的资料和方法论， 值得一试。",
				imgs4, "", "8小时前");
		for (int i = 0; i < 1; i++) {
			msgList.add(item4);
		}
	}

	private class CircleAdapter extends BaseAdapter implements CircleItemView.OnCommentListener,
			CircleItemView.OnItemClickListener, ImageAdapter.OnGridViewClickListener {

		private Context context;
		private ArrayList<CircleItem> items;
		@SuppressLint("UseSparseArrays")
		private Map<Integer, CircleItemView> cacheViews = new HashMap<Integer, CircleItemView>();

		public CircleAdapter(Context context, ArrayList<CircleItem> items) {
			super();
			this.context = context;
			this.items = items;
		}

		@SuppressLint("InflateParams")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view;

			if (convertView != null) {
				view = convertView;
			} else {
				view = LayoutInflater.from(context).inflate(R.layout.listview_item_layout, null, false);
			}

			if (view instanceof CircleItemView) {
				CircleItem itemData = (CircleItem) getItem(position);
				((CircleItemView) view).setItemData(itemData);
				((CircleItemView) view).setPosition(position);
				((CircleItemView) view).setCommentListener(this);
				((CircleItemView) view).setItemClickListener(this);
				((CircleItemView) view).setGridViewClickListener(this);

				cacheView(position, (CircleItemView) view);
			}

			return view;
		}

		private void cacheView(int position, CircleItemView view) {
			Iterator<Map.Entry<Integer, CircleItemView>> entries = cacheViews.entrySet().iterator();

			while (entries.hasNext()) {
				Entry<Integer, CircleItemView> entry = entries.next();
				if (entry.getValue() == view && entry.getKey() != position) {
					cacheViews.remove(entry.getKey());
					break;
				}
			}

			cacheViews.put(position, view);
		}

		public Object getItemCache(int position) {
			return cacheViews.get(position);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return items.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return items.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public void onComment(int position) {
			// TODO Auto-generated method stub
			showCommentView(position);
		}

		@Override
		public void onLike(int position) {
			// TODO Auto-generated method stub
			CircleItem item = (CircleItem) getItem(position);
			Toast.makeText(context, "点赞", Toast.LENGTH_LONG).show();
			items.get(position).getLikes().add("小龙女");

			CircleItemView itemView = (CircleItemView) getItemCache(position);
			if (itemView != null && position == itemView.getPosition()) {
				itemView.addComment();
			}
		}

		@Override
		public void onAvatarClick(int position) {
			// TODO Auto-generated method stub
			CircleItem item = (CircleItem) getItem(position);
			Toast.makeText(context, item.getImgHeader(), Toast.LENGTH_LONG).show();
		}

		@Override
		public void onNameClick(int position) {
			// TODO Auto-generated method stub
			CircleItem item = (CircleItem) getItem(position);
			Toast.makeText(context, item.getTxtName(), Toast.LENGTH_LONG).show();
		}

		private void showCommentView(final int position) {
			commentView.setVisibility(View.VISIBLE);
			commentView.findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					EditText et = (EditText) commentView.findViewById(R.id.edit);
					String s = et.getText().toString();

					if (!TextUtils.isEmpty(s)) {
						CircleComment comment = new CircleComment("我", s);
						items.get(position).getComments().add(comment);

						CircleItemView itemView = (CircleItemView) getItemCache(position);
						if (itemView != null && position == itemView.getPosition()) {
							itemView.addComment();
						}

						et.setText("");
						commentView.setVisibility(View.GONE);
					}
				}
			});
		}

		@Override
		public void onGridViewCilcked(int position, ArrayList<String> items) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(MainActivity.this, GalleryActivity.class);
			intent.putExtra("items", items);
			intent.putExtra("index", position);
			startActivity(intent);
		}
	}
}
